package com.yuhong.reggie.controller;

import com.yuhong.reggie.common.R;
import com.yuhong.reggie.entity.Category;
import com.yuhong.reggie.entity.Page;
import com.yuhong.reggie.service.CategoryService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //根据类型查询类别的数据
    @RequestMapping("/list")
    public R<List<Category>> list(Integer type){
        //调用业务方法
        List<Category> allByType = categoryService.findAllByType(type);
        //封装数据
        return R.success(allByType);
    }


    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        response.setHeader("content-disposition","attachment;filename=category.xlsx");

        //1. 读取模板的输入流
        InputStream inputStream = this.getClass().getResourceAsStream("/excel/category.xlsx");

        //2。 使用模板的输入流构建一个工作薄
        Workbook workbook = new XSSFWorkbook(inputStream);

        //3. 得到工作单
        Sheet sheet = workbook.getSheetAt(0);

        //4. 读取第一行,提取第一行的样式，并且存储到集合中
        Row row = sheet.getRow(1);
        List<CellStyle> cellStylesList= new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            //得到单元格的样式
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStylesList.add(cellStyle);
        }

        //5. 得到数据,遍历数据，把数据写入到excel
        List<Category> categoryList = categoryService.findAll();
        for (int i = 0; i < categoryList.size(); i++) {
            //一个类别就对应一行
            row =  sheet.createRow(i+1);
            Category category = categoryList.get(i);
            //把类别信息设置到单元格上

            //类别类型
            Cell cell = row.createCell(0);
            //设置单元格内容
            cell.setCellValue(category.getType());
            cell.setCellStyle(cellStylesList.get(0));

            //类别名字
            cell = row.createCell(1);
            //设置单元格内容
            cell.setCellValue(category.getName());
            cell.setCellStyle(cellStylesList.get(1));


            //类别排序
            cell = row.createCell(2);
            //设置单元格内容
            cell.setCellValue(category.getSort());
            cell.setCellStyle(cellStylesList.get(2));

            //创建时间
            cell = row.createCell(3);
            //设置单元格内容
            cell.setCellValue(category.getCreateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(3));


            //创建时间
            cell = row.createCell(4);
            //设置单元格内容
            cell.setCellValue(category.getUpdateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(4));
        }
        //6. 把工作薄写出
        workbook.write(response.getOutputStream());
    }

    @RequestMapping
    public R<String> save(@RequestBody Category category, HttpSession session){
        //获取登录用户id
        Long employeeId = (Long) session.getAttribute("employee");
        //完善创建人
        category.setCreateUser(employeeId);
        //完善修改时间
        category.setUpdateUser(employeeId);
        //调用业务添加
        categoryService.save(category);
        return R.success("成功");
    }
    @GetMapping("/page")
    public R<Page<Category>> findAll(Integer page, Integer pageSize){
        Page<Category> page1 = categoryService.findByPage(page, pageSize);

        return  R.success(page1);

    }
    @DeleteMapping
    public R<String> detele(Long id){
    categoryService.delete(id);
    return R.success("删除成功");
    }

    @PutMapping
    public R<String> updeta(@RequestBody Category category,HttpSession session){
        Long employee = (Long) session.getAttribute("employee");
        //完善修改人
        category.setUpdateUser(employee);

        categoryService.update(category);

        return R.success("修改成功");
    }
}
